package com.badminton.web.model.response;

import lombok.Data;

import java.util.ArrayList;

/**
 * 此实体为通用后端反馈前端实体对象
 {
    "isSuccess":"true/false", --根据这个区分业务成功与否
    "code":"00001",--这个是后端的errorCode,便于后端快速定位问题
    "msg":"snippet不存在 ", --这个是前端需要提示的信息
    "data":"Array<>[]", --这个类似于查询反馈的结果
 }
 */
@Data
public class BaseResponse<T> {
    //成功默认状态码
    public static final Integer SUCCESS_CODE = 200;
    //失败默认状态码
    public static final Integer FAIL_CODE = 100;
    //成功默认描述
    public static final String SUCCESS_MSG = "请求成功";
    //失败默认描述
    public static final String FAIL_MSG = "请求失败";

    private Boolean result;
    private int code;
    private String message;
    private T data;

    public static BaseResponse success() {
        return new BaseResponse(true, BaseResponse.SUCCESS_CODE, BaseResponse.SUCCESS_MSG, new ArrayList());
    }

    public static BaseResponse success(String msg) {
        return new BaseResponse(true, BaseResponse.SUCCESS_CODE, msg, new ArrayList());
    }

    public static BaseResponse success(Object data) {
        return new BaseResponse(true, BaseResponse.SUCCESS_CODE, BaseResponse.SUCCESS_MSG, data);
    }

    public static BaseResponse success(String msg, Object data) {
        return new BaseResponse(true, BaseResponse.SUCCESS_CODE, msg, data);
    }

    public static BaseResponse fail(String msg) {
        return new BaseResponse(false, BaseResponse.FAIL_CODE, msg, new ArrayList());
    }

    public static BaseResponse fail(String msg, Object data) {
        return new BaseResponse(false, BaseResponse.FAIL_CODE, msg, data);
    }

    public static BaseResponse fail(int code, String msg) {
        return new BaseResponse(false, code, msg, new ArrayList());
    }

    public BaseResponse(Boolean isSuccess, int code, String msg, T data) {
        this.result = isSuccess;
        this.code = code;
        this.message = msg;
        this.data = data;
    }


//    @Override
//    public String toString() {
//        return "BaseResponse{" +
//                "isSuccess=" + isSuccess +
//                "code=" + code +
//                ", msg='" + msg + '\'' +
//                ", data=" + data +
//                '}';
//    }
}
